package com.task.manage.system.service.impl;

import com.task.manage.system.domain.MerchantTask;
import com.task.manage.system.domain.MerchantTaskPriority;
import com.task.manage.system.domain.TaskManage;
import com.task.manage.system.domain.resp.RecoryTaskResp;
import com.task.manage.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RecoryTaskServiceImpl implements RecoryTaskService {
    @Autowired
    private ITaskManageService taskManageService;

    @Resource
    private IMerchantTaskService merchantTaskService;

    @Resource
    private ITaskReveiveRecordService taskReveiveRecordService;

    @Resource
    private IMerchantTaskPriorityService taskPriorityService;


    @Override
    @Transactional
    public StringBuffer recoryTask(RecoryTaskResp recoryTaskResp,List<Long> idsList) {
        StringBuffer stringBuffer=new StringBuffer();
        //回收任务的id
        Long  merchantTaskId= recoryTaskResp.getMerchantTaskId();
        //待回收的次数
        int counts=recoryTaskResp.getCounts();
        MerchantTask merchantTask= merchantTaskService.selectMerchantTaskById(merchantTaskId);
        merchantTask.setTaskCounts(merchantTask.getTaskCounts()+counts);
        //更新任务次数
        merchantTaskService.updateMerchantTask(merchantTask);
        //删除任务记录
        List<TaskManage> manageList=taskManageService.selectTaskManageByIds(idsList);
        List<String> taskIdList= manageList.stream()
                .map(taskManage -> String.valueOf(taskManage.getTaskRecordId()))
                .collect(Collectors.toList());
        String idss=String.join(",", taskIdList);
        taskReveiveRecordService.deleteTaskReveiveRecordByIds(idss);
        //删除任务管理器
        String idsManage=idsList.stream()
                .map(Object::toString)
                .collect(Collectors.joining(","));
        taskManageService.deleteTaskManageByIds(idsManage);
        //任务优先级表的优先级减1
        MerchantTaskPriority merchantTaskPriority= taskPriorityService.getMerchantTaskPriority(merchantTaskId);
        if(merchantTaskPriority!=null&&merchantTaskPriority.getPriorityNum()>0){
            merchantTaskPriority.setPriorityNum(merchantTaskPriority.getPriorityNum()-1);
            taskPriorityService.updateMerchantTaskPriority(merchantTaskPriority);
        }
        return stringBuffer;
    }

    @Override
    public void deleteTask(RecoryTaskResp recoryTaskResp, List<Long> idsList) {
        //回收任务的id
        Long  merchantTaskId= recoryTaskResp.getMerchantTaskId();
        //删除任务记录
        List<TaskManage> manageList=taskManageService.selectTaskManageByIds(idsList);
        List<String> taskIdList= manageList.stream()
                .map(taskManage -> String.valueOf(taskManage.getTaskRecordId()))
                .collect(Collectors.toList());
        String idss=String.join(",", taskIdList);
        taskReveiveRecordService.deleteTaskReveiveRecordByIds(idss);
        //删除任务管理器
        String idsManage=idsList.stream()
                .map(Object::toString)
                .collect(Collectors.joining(","));
        taskManageService.deleteTaskManageByIds(idsManage);
        //任务优先级表的优先级减1
        MerchantTaskPriority merchantTaskPriority= taskPriorityService.getMerchantTaskPriority(merchantTaskId);
        if(merchantTaskPriority!=null&&merchantTaskPriority.getPriorityNum()>0){
            merchantTaskPriority.setPriorityNum(merchantTaskPriority.getPriorityNum()-1);
            taskPriorityService.updateMerchantTaskPriority(merchantTaskPriority);
        }
    }
}
